﻿using Microsoft.Owin.Security.OpenIdConnect;
using Sidvall.Web.Security;
using Sidvall.WorkItemManager.Web.Host.Models;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Sidvall.WorkItemManager.Web.Host.Controllers
{
	public class HomeController : Controller
	{
		#region Public Members

		#region Index

		[HttpGet]
		[Route("")]
		[Route("Index")]
		public ActionResult Index()
		{
			IndexModel model;

			model = new IndexModel();

			return View(model);
		}

		#endregion
		#region Login

		[AuthenticationMethodAuthorizeAttribute]
		[HttpGet]
		[Route("Login")]
		public ActionResult Login()
		{
			IndexModel model;

			model = new IndexModel();

			return View("Index", model);
		}

		#endregion
		#region LogOff

		[AuthenticationMethodAuthorizeAttribute]
		[HttpPost]
		[Route("LogOff")]
		public ActionResult LogOff()
		{
			IndexModel model;

			Sidvall.SystemContext.Current.PrincipalContextManager.SetSessionValue("LoggingOff", true);
			System.Web.HttpContext.Current.GetOwinContext().Authentication.SignOut(OpenIdConnectAuthenticationDefaults.AuthenticationType);
			model = new IndexModel();

			return View("Index", model);
		}

		#endregion

		#region WebApi

		[HttpGet]
		[Route("WebApi", Name = "ShowWebApi")]
		public async Task<ActionResult> WebApi()
		{
			var model = new WebApiModel();
			await model.LoadAsync();
			return View(model);
		}

		#endregion

		#endregion
	}
}